package com.luocj.elephant.demo.activity;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.luocj.elephant.demo.R;
import com.luocj.elephant.demo.utils.Utils;

public class IndicatorActivity extends AppCompatActivity {

    private static final String TAG = IndicatorActivity.class.getSimpleName();
    private View indicator;
    private RecyclerView recyclerview;
    private Context mContext;
    private int indicatorWidth;
    private int screenWidth;
    private int disX;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_indicator);
        mContext = this;
        recyclerview = findViewById(R.id.recyclerview);

        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false);
        recyclerview.setLayoutManager(linearLayoutManager);

        LinearSnapHelper snapHelper = new LinearSnapHelper();
        snapHelper.attachToRecyclerView(recyclerview);
        MyAdapter adapter = new MyAdapter(mContext);
        recyclerview.setAdapter(adapter);
        indicator = findViewById(R.id.indicator);

        screenWidth = Utils.getScreenWidth(mContext);
        Log.i(TAG, "onCreate: " + screenWidth);

        getIndicatorWidth(indicator);
        recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
//                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
//                LinearLayout rootview = (LinearLayout) layoutManager.findViewByPosition(0);
//                if (rootview != null) {
//
//                    Log.i(TAG, "onScrolled:  percent" + rootview.getLeft());
//                    if (dx < 0) {
////                        //右边滑动
////                        indicator.setTranslationX();
////                        Log.i(TAG, "onScrolled: " );
//                        indicator.scrollTo(-50,0);
//
//                    } else {
//                    float percent = (Math.abs(rootview.getLeft())) / (screenWidth * 1.f);
//                        //左边滑动
//                        Log.i(TAG, "onScrolled: " + "getLeft---------------> " + rootview.getLeft());
//                        indicator.scrollTo(50,0);
//
//                    }
//                }

                int i = recyclerView.computeHorizontalScrollExtent();
                int i1 = recyclerView.computeHorizontalScrollRange();
                float dimension = getResources().getDimension(R.dimen.x30);
                float v;
                if (disX < 0){
                    disX = 0;
                    v = 0;
                }else {
                    v = dimension * ((float) dx / (i1-i));
                }

                disX += v;
                ObjectAnimator.ofFloat(indicator,"translationX",disX , disX +v,50).start();
            }
        });

    }

    /**
     * 获取宽高
     * @param indicator
     */
    private void getIndicatorWidth(final View indicator) {
        indicator.post(new Runnable(){
            @Override
            public void run(){
                indicatorWidth = indicator.getMeasuredWidth();
                int indicatorHeight=indicator.getMeasuredHeight();
            }
        });
    }

    private class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {
        private Context mContext;

        public MyAdapter(Context context) {
            this.mContext = context;
        }

        @NonNull
        @Override
        public MyAdapter.Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(mContext).inflate(R.layout.item, parent, false);
            Holder holder = new Holder(inflate);
            return holder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyAdapter.Holder holder, int position) {

        }

        @Override
        public int getItemCount() {
            return 2;
        }

        public class Holder extends RecyclerView.ViewHolder {
            public Holder(View itemView) {
                super(itemView);
            }
        }
    }
}
